package com.hyc.myvideo.ui.activity.home;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import com.acmenxd.toaster.Toaster;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.hyc.myvideo.R;
import com.hyc.myvideo.data.api.model.BaseResult;
import com.hyc.myvideo.data.api.model.CurriculumHour;
import com.hyc.myvideo.ui.base.BaseFActivity;
import com.hyc.myvideo.ui.service.OnAudioEventListener;
import com.hyc.myvideo.utils.PlayerUtils;
import com.hyc.utils.StringUtils;
import java.util.ArrayList;
import java.util.List;
import timber.log.Timber;

import static android.R.attr.type;

/**
 * 音频播放界面
 * Created by dfeads on 2017/7/14.
 */

public class AudioActivity extends BaseFActivity {
  /** 播放 */
  @BindView(R.id.activity_audio_play) ImageView mPlay;
  /** 标题 */
  @BindView(R.id.activity_audio_title) TextView mTitle;
  /** 练习人数 */
  @BindView(R.id.activity_audio_num) TextView mNum;
  /** 下面的标题 */
  @BindView(R.id.activity_audio_title_two) TextView mTitleTwo;
  /** 循环控制 */
  private boolean isLoop = false;
  /** 开始时间 */
  @BindView(R.id.activity_audio_start) TextView mAudioStart;
  /** 更多简介 */
  @BindView(R.id.activity_audio_more) TextView mMore;
  /** 结束时间 */
  @BindView(R.id.activity_audio_end) TextView mAudioEnd;
  /** 循环 */
  @BindView(R.id.activity_audio_loop) TextView mLoop;

  @BindView(R.id.activity_audio_seek_bar) SeekBar mSeekBar;

  private List<CurriculumHour> curriculumHourList;

  private CurriculumHour curriculumHour = null;
  private String curriculum_hour_id = "";
  private int positiion = 0;

  @Override public int getContentViewId() {
    return R.layout.activity_audio;
  }

  @Override protected void initView() {
    mSeekBar.setProgress(0);
    mPlay.setSelected(!audioService.isPlaying());
    if (audioService.isPlaying()) {
      mSeekBar.setMax(audioService.getTotalTime());
    }
    curriculumHourList = new ArrayList<>();
    mMore.setVisibility(View.VISIBLE);
    mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
      @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
      }

      @Override public void onStartTrackingTouch(SeekBar seekBar) {
      }

      @Override public void onStopTrackingTouch(SeekBar seekBar) {
        if (audioService.isPlaying()) {
          int progress = seekBar.getProgress();
          audioService.seekTo(progress);
        }
      }
    });

    audioService.setOnAudioEventListener(new OnAudioEventListener() {
      @Override public void onPublish(int progress) {
        if (!isFinishing()) {
          mAudioStart.setText(PlayerUtils.converLongTimeToStr(progress));
          mSeekBar.setProgress(progress);
        }
      }

      @Override public void onMaxPro(int totalTime) {
        if (!isFinishing()) {
          mSeekBar.setMax(totalTime);
          mAudioEnd.setText(PlayerUtils.converLongTimeToStr(totalTime));
        }
      }

      @Override public void onBufferingUpdate(int percent) {
        if (!isFinishing()) {
          mSeekBar.setSecondaryProgress(percent);
        }
      }

      @Override public void onPlayerPause() {

      }

      @Override public void onPlayerResume() {

      }
    });
  }

  @Override protected void initData() {
    curriculum_hour_id = getIntent().getStringExtra("id");
    if (!StringUtils.isEmpty(curriculum_hour_id)) {
      api.getHomeClassHourDetails(curriculum_hour_id);
    }
  }

  @Override protected void oNext(String resulte, String method) {
    BaseResult<CurriculumHour> curriculums =
        JSONObject.parseObject(resulte, new TypeReference<BaseResult<CurriculumHour>>() {
        });
    if (curriculums.isCode()) {
      curriculumHour = curriculums.getResult();
      CurriculumHour curriculumHour1 = new CurriculumHour();
      curriculumHour1.setCurriculum_hour_title("丑八怪");
      curriculumHour1.setCurriculum_hour_url(
          "http://fs.web.kugou.com/5ef8cd0dab309f24fa042de32f99c0f7/598cf979/G009/M02/06/15/SQ0DAFUJf-KAa_LnAD3gDgbvu7o702.mp3");
      CurriculumHour curriculumHour2 = new CurriculumHour();
      curriculumHour2.setCurriculum_hour_title("演员");
      curriculumHour2.setCurriculum_hour_url(
          "http://fs.web.kugou.com/e0b0728dec66ed8f7efa0021c0e3cd4e/598d001d/G018/M00/0A/04/Ug0DAFVw-LCAYoUkAD_FoLiLEPc990.mp3");
      CurriculumHour curriculumHour3 = new CurriculumHour();
      curriculumHour3.setCurriculum_hour_title("动物世界");
      curriculumHour3.setCurriculum_hour_url(
          "http://fs.web.kugou.com/b324a655a1cff95de8f8829b48a01b66/598cfd22/G102/M08/12/10/BocBAFjaR1mAa6-ZADhI4z78XoQ686.mp3");
      curriculumHourList.add(curriculumHour1);
      curriculumHourList.add(curriculumHour2);
      curriculumHourList.add(curriculumHour3);
      audioService.setCurriculumHourList(curriculumHourList);
      audioService.play(0);
      Timber.e("======curriculumHour==play=====");
      mPlay.setSelected(false);
      setView();
    } else {
      Toaster.show(curriculums.getMsg());
    }
  }

  private void setView() {
    if (curriculumHour == null) {
      return;
    }
    mTitle.setText(curriculumHour.getCurriculum_hour_title());
    mTitleTwo.setText(curriculumHour.getCurriculum_hour_title());
    mNum.setText(curriculumHour.getCurriculum_people_count() + "人已练习");
  }

  @Override protected void oError() {

  }

  @OnClick({
      R.id.activity_audio_more, R.id.activity_audio_play, R.id.activity_audio_back,
      R.id.activity_audio_comment, R.id.activity_audio_add, R.id.activity_audio_list,
      R.id.activity_audio_pre, R.id.activity_audio_next
  }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.activity_audio_more://更多
        if (curriculumHour != null) {
          startActivity(new Intent(AudioActivity.this, AudioMoreActivity.class).putExtra("id",
              curriculumHour.getCurriculum_hour_id()));
        }
        break;
      case R.id.activity_audio_play://播放
        mPlay.setSelected(audioService.isPlaying());
        audioService.playPause();
        break;
      case R.id.activity_audio_pre://上一曲
        audioService.pre();
        mPlay.setSelected(audioService.isPlaying());
        break;
      case R.id.activity_audio_next://下一曲
        audioService.next();
        mPlay.setSelected(audioService.isPlaying());
        break;
      case R.id.activity_audio_back://返回
        finish();
        break;
      case R.id.activity_audio_list://列表
        startActivity(new Intent(AudioActivity.this, AudioListActivity.class));
        break;
      case R.id.activity_audio_comment://评论
        if (curriculumHour != null) {
          startActivity(new Intent(AudioActivity.this, AudioMoreActivity.class).putExtra("id",
              curriculumHour.getCurriculum_hour_id()).putExtra("type", 1));
        }
        //startActivity(new Intent(AudioActivity.this, LeavingMsgActivity.class));
        break;
      case R.id.activity_audio_add://添加
        startActivity(
            new Intent(AudioActivity.this, TrainingCatalogSecActivity.class).putExtra("id",
                curriculumHour.getCurriculum_id()));
        break;
    }
  }
}
